package com.xiaoxiangxueyuan.chapters01;

import java.util.LinkedList;
import java.util.Queue;

/**
 * 两个队列实现一
 */
public class TwoQueueImplementStack {
    private static Queue<Integer> dataQueue = new LinkedList<>();
    private static Queue<Integer> tempQueue = new LinkedList<>();

    public static void main(String[] args) {
        TwoQueueImplementStack stack = new TwoQueueImplementStack();
        stack.push(1);
        stack.push(2);
        stack.push(3);
        stack.push(4);
        stack.push(5);
        System.out.println("栈的大小" + stack.size());
        while (!stack.empty()) {
            System.out.println(stack.pop());
        }
    }

    /**
     * 入栈
     */
    public void push(Integer item) {
        tempQueue.offer(item);
        while (!dataQueue.isEmpty()) {
            tempQueue.offer(dataQueue.poll());
        }
        while (!tempQueue.isEmpty()) {
            dataQueue.offer(tempQueue.poll());
        }
    }

    /**
     * 出栈
     *
     * @return
     */
    public Integer pop() {
        return dataQueue.poll();
    }

    /**
     * 求栈的大小
     */
    public Integer size() {
        return dataQueue.size();
    }

    /**
     * 栈是否是空的
     *
     * @return
     */
    public boolean empty() {
        return dataQueue.isEmpty();
    }
}
